<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/13
 * Time: 9:43
 */
namespace app\mtadminapi\controller;

use app\command\controller\Common;
use app\mtadminapi\model\MtShopHouseRebatehouse;
use app\util\ReturnCode;

/**
 * 房屋营销管理
 * Class AdminSysMarkeManage
 * @package app\mtadminapi\controller
 */
class AdminSysMarkeManage extends Base
{
    public function __construct(){
        parent::__construct($this->request);
    }

    /**
     * 房屋今日优惠添加
     * @return array
     */
    public function saleToday(){
        $rule = [
            "houseID" => "require|number",
            "startTime" => "require",
            "rebate" => "require|number",
            "final" => "require|number",
            "finalRebate" => "number"
        ];
        $msg = [];
        $houseData = $this->request->post();
        $result = (new Common())->VerifiString($rule,$msg,$houseData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        $res = (new MtShopHouseRebatehouse())->insert($houseData,true);
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"入库失败");
        }
        return $this->bindSuccess("添加成功");
    }

    /**
     * 今日优惠修改
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function houseMarkeSave(){
        $rule = [
            "houseID" => "number",
            "rebate" => "number",
            "final" => "number",
            "finalRebate" => "number",
            "status" => "number"
        ];
        $msg = [];
        $houseData = $this->request->post();
        $result = (new Common())->VerifiString($rule,$msg,$houseData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        $res = (new MtShopHouseRebatehouse())->where("houseID",$houseData['houseID'])->update($houseData);
        if(!$res){
            return $this->bindError(ReturnCode::DATA_EXISTS,"失败");
        }
        return $this->bindSuccess("添加成功");
    }
}